<?php

/**
 * Обеспечение доступа к данным в модуле authorization
 */
class Authorization_Model_Authorization {

    public $user = array();

    public function login($username, $password) {
        $authAdapter = new Zend_Auth_Adapter_DbTable(
                        Zend_Registry::get('db'),
                        'Users',
                        'Login',
                        'Password'
        );

        $authAdapter->setIdentity($username)->setCredential($password);
        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);

        if ($result->isValid()) {
            $users_model = new Models_Users();
            $user = $users_model->fetchRow(array(
                        'Login = ?' => $authAdapter->getResultRowObject()->Login
                    ));
            $role = $user->findParentRow(get_class(new Models_Roles));
            $this->user = array(
                'UserId' => $user->UserId,
                'Login' => $user->Login,
                'RoleId' => $user->RoleId,
                'Role' => $role->Name
            );
            $auth->getStorage()->write($this->user);
        }

        return $result->isValid();
    }

    public function logout() {
        Zend_Auth::getInstance()->clearIdentity();
    }

}